package com.rongke.web.controller.medical;

import com.alibaba.fastjson.JSONObject;
import com.rongke.commons.JsonResp;
import com.rongke.model.Radiotherapy;
import com.rongke.service.RadiotherapyService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 放疗记录
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/radiotherapy")
@Transactional
public class RadiotherapyController {
	private static final Logger LOGGER = Logger
			.getLogger(RadiotherapyController.class);

	@Autowired
	private RadiotherapyService radiotherapyService;

	/**
	 * @添加放疗记录
	 * @param radiotherapy
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResp addRadiotherapy(@RequestBody Radiotherapy radiotherapy) {
		LOGGER.debug("addRadiotherapy");

		try {
			radiotherapyService.insert(radiotherapy);

			JSONObject json = new JSONObject();
			json.put("id", radiotherapy.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("addRadiotherapy failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @修改放疗记录
	 * @param radiotherapy
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResp updateRadiotherapy(@RequestBody Radiotherapy radiotherapy) {
		LOGGER.debug("updateRadiotherapy");

		try {
			radiotherapy.setGmtCreate(null);// 不修改创建时间
			radiotherapy.setGmtModified(new Date());
			radiotherapy.setPicContent(null);
			radiotherapy.setUserId(null);
			radiotherapy.setMedicalRecordId(null);

			radiotherapyService.updateById(radiotherapy);

			JSONObject json = new JSONObject();
			json.put("id", radiotherapy.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("updateRadiotherapy failed", e);
			return JsonResp.fa(null);
		}
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public JsonResp delRadiotherapyById(long id) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("delRadiotherapyById, id=" + id);
		}

		try {
			radiotherapyService.deleteById(id);

			JSONObject json = new JSONObject();
			json.put("id", id);
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("delRadiotherapyById failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @根据id查找放疗记录
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectRadiotherapy(Long id) {
		LOGGER.debug("selectRadiotherapy, id=" + id);

		try {
			Radiotherapy radiotherapy = radiotherapyService.selectById(id);
			return JsonResp.ok(radiotherapy);
		} catch (Exception e) {
			LOGGER.error("selectRadiotherapy failed", e);
			return JsonResp.fa(null);
		}
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResp selectList(long medicalRecordId) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("selectList, medicalRecordId=" + medicalRecordId);
		}

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("medical_record_id", medicalRecordId);
			List<Radiotherapy> result = radiotherapyService.selectByMap(params);
			return JsonResp.ok(result);
		} catch (Exception e) {
			LOGGER.error("selectList failed, medicalRecordId="
					+ medicalRecordId, e);
			return JsonResp.fa(null);
		}
	}
}
